package com.bosssoft.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.entity.dto.FibIndicatorsDTO;
import com.bosssoft.entity.vo.FibIndicatorsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bosssoft.mapper.FibIndicatorsMapper;
import com.bosssoft.entity.po.FibIndicatorsPO;
import com.bosssoft.service.FibIndicatorsService;

import javax.annotation.Resource;

/**
 * @author Li kexi
 * Created on 2021/8/20.17:42
 */
@Service
@Slf4j
public class FibIndicatorsServiceImpl extends ServiceImpl<FibIndicatorsMapper, FibIndicatorsPO> implements FibIndicatorsService {

    @Resource
    private FibIndicatorsMapper fibIndicatorsMapper;

    @Override
    public int updateBatch(List<FibIndicatorsPO> list) {
        return baseMapper.updateBatch(list);
    }

    @Override
    public int updateBatchSelective(List<FibIndicatorsPO> list) {
        return baseMapper.updateBatchSelective(list);
    }

    @Override
    public int batchInsert(List<FibIndicatorsPO> list) {
        return baseMapper.batchInsert(list);
    }

    @Override
    public int insertOrUpdate(FibIndicatorsPO record) {
        return baseMapper.insertOrUpdate(record);
    }

    @Override
    public int insertOrUpdateSelective(FibIndicatorsPO record) {
        return baseMapper.insertOrUpdateSelective(record);
    }

    @Override
    public Page<FibIndicatorsVO> selectAllByPage(FibIndicatorsVO fibIndicatorsVO,Integer current, Integer size) {
        Page<FibIndicatorsPO> page = new Page<>(current, size);
        QueryWrapper<FibIndicatorsPO> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(fibIndicatorsVO.getIndId())) {
            queryWrapper.like("ind_id", fibIndicatorsVO.getIndId());
        }
        Page<FibIndicatorsPO> poiPage = fibIndicatorsMapper.selectPage(page, queryWrapper);
        Page<FibIndicatorsVO> voiPage = new Page<>(current, size);
        BeanUtils.copyProperties(poiPage, voiPage);
        return voiPage;
    }

    @Override
    public Page<FibIndicatorsVO> selectByStatus(Integer current, Integer size, Integer status) {
        QueryWrapper<FibIndicatorsPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", status);
        Page<FibIndicatorsPO> page = new Page<>(current, size);
        Page<FibIndicatorsPO> poiPage = fibIndicatorsMapper.selectPage(page, queryWrapper);
        Page<FibIndicatorsVO> voiPage = new Page<>(current, size);
        BeanUtils.copyProperties(poiPage, voiPage);
        return voiPage;
    }

    @Override
    public Page<FibIndicatorsVO> selectByType(Integer current, Integer size, Integer indType) {
        QueryWrapper<FibIndicatorsPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ind_type", indType);
        Page<FibIndicatorsPO> page = new Page<>(current, size);
        Page<FibIndicatorsPO> poiPage = fibIndicatorsMapper.selectPage(page, queryWrapper);
        Page<FibIndicatorsVO> voiPage = new Page<>(current, size);
        BeanUtils.copyProperties(poiPage, voiPage);
        return voiPage;
    }

    @Override
    public int inputIndicators(FibIndicatorsDTO fibIndicatorsDTO) {

        log.info("开始录入指标");
        FibIndicatorsPO fibIndicatorsPO = new FibIndicatorsPO();
        BeanUtils.copyProperties(fibIndicatorsDTO, fibIndicatorsPO);
        //fibIndicatorsMapper.insert(fibIndicatorsPO);
        if (fibIndicatorsPO.getIndId() == null) {
            int i = fibIndicatorsMapper.insert(fibIndicatorsPO);
        }
        int i = fibIndicatorsMapper.updateById(fibIndicatorsPO);
        return i;
    }

    @Override
    public boolean auditPass(Long id, Integer status) {

        // 要判断指标类型
        FibIndicatorsPO indicators = fibIndicatorsMapper.selectById(id);
        indicators.setStatus(status);

        if (indicators.getIndType().equals(2)) {

            // 对单位指标：改状态，修改父级处室指标的在途金额，已审金额
            FibIndicatorsPO pIndicators = fibIndicatorsMapper.selectById(indicators.getIndPid());

            //原在途金额减去审核通过的金额
            pIndicators.setIndWayAmount(pIndicators.getIndWayAmount()
                    .subtract(indicators.getIndWayAmount()));
            //原已审金额加上审核通过的金额
            pIndicators.setAuditedAmount(pIndicators.getAuditedAmount()
                    .add(indicators.getIndWayAmount()));
            updateById(pIndicators);

        }
        // 对处室指标不作特殊处理
        boolean b = updateById(indicators);
        return b;
    }

    @Override
    public boolean auditReject(Long id, Integer status) {

        // 要判断指标类型
        FibIndicatorsPO indicators = fibIndicatorsMapper.selectById(id);
        indicators.setStatus(status);
        if (indicators.getIndType().equals(2)) {
            // 对单位指标：改状态，修改父级指标 分配金额退回，在途金额退回，已审金额不变
            FibIndicatorsPO pIndicators = fibIndicatorsMapper.selectById(indicators.getIndPid());

            //分配金额退回
            pIndicators.setIndAllot(pIndicators.getIndAllot()
                    .subtract(indicators.getIndAllot()));

            //在途金额退回
            pIndicators.setIndWayAmount(pIndicators.getIndWayAmount()
                    .subtract(indicators.getIndAllot()));
        }
        // 对处室指标:不做特殊处理
        boolean b = updateById(indicators);
        return b;
    }

    @Override
    public boolean distribution(FibIndicatorsDTO indicatorsDTO) {


        // 先对指标进行录入
        int b = inputIndicators(indicatorsDTO);
        if (b == 0) {
            return false;
        }
        Long pid = indicatorsDTO.getIndPid();
        FibIndicatorsPO pIndicator = fibIndicatorsMapper.selectById(pid);
        //父级指标 分配金额增加
        pIndicator.setIndAllot(pIndicator.getIndAllot()
                .add(indicatorsDTO.getIndAllot()));
        //父级指标 在途金额增加
        pIndicator.setIndWayAmount(pIndicator.getIndWayAmount()
                .add(indicatorsDTO.getIndAllot()));
        // 更新父指标
        boolean result = updateById(pIndicator);
        return result;
    }


}
